function addEvent() {
	var oMore = document.getElementById('more');
	var oToplist = document.getElementById('toplist');
	oToplist.style.display = "none";
	if(oMore) {
		oMore.onclick = function() {
			var style = oToplist.style;
			style.display = style.display == "none" ? "block" : 'none';
		}
	}
}
addEvent();

$(function(){
	var curr_page = 1;
	$('#s_page a').click(function(){
		var val = parseInt($(this).attr('val'));
		switch(val){
			case 1:
				curr_page=1;
				break;
			case 2:
				if(curr_page<=1)
				{
					return false;
				}
				curr_page-=1;
				break;
			case 3:
				if(curr_page>=pages)
				{
					return false;
				}
				curr_page+=1;
				break;
			case 4:
				if(curr_page>=pages)
				{
					return false;
				}
				curr_page=pages;
				break;
		}
		load_page();
	});
	
	function submit(){
		var obj = $(this);
		var content = $.trim($('#content').val());
	    if(content=='') {
			alert('内容不能为空');
			return false;
		}
		return true;
	}
	$("#commentForm").submit(submit);
	$("#commentForm").show();

	var n_page_val = ''; 
	$('#n_page').focus(function(){
		n_page_val = $(this).val();
		$(this).val('');	
	});
	$('#n_page').blur(function(){
		var val = $(this).val().replace(/^\s+|\s+$/,'');
		if(/[^\d]/.test(val) || val=='' || val==curr_page)
		{
			$(this).val(n_page_val);
			return false;	
		}	
		if(val<1)
		{
			curr_page = 1;
			$(this).val('1/'+pages);
		}
		else if(val>pages)
		{
			curr_page = pages;
			$(this).val(pages+'/'+pages);
		}
		else
		{
			curr_page = val;
			$(this).val(val+'/'+pages);
		}
		load_page();
	});
	function load_page(){
		loader(1);
		$.get(commentsurl,{"page":curr_page},function(data){
			if($.trim(data).length > 0) {
				$('#s_leaveme').html(data);
				$('#n_page').val(curr_page+'/'+pages);
			}
			page_link();
			loader(2);
		});
	}
	function page_link()
	{
		if(curr_page>=pages)
		{
			$('#s_page a').eq(2).css('color','#cbcbcb');
			$('#s_page a').eq(3).css('color','#cbcbcb');
		}
		else if(pages>1)
		{
			$('#s_page a').eq(2).css('color','#333');
			$('#s_page a').eq(3).css('color','#333');
		}
		if(curr_page>1)
		{
			$('#s_page a').eq(0).css('color','#333');
			$('#s_page a').eq(1).css('color','#333');
		}else
		{
			$('#s_page a').eq(0).css('color','#cbcbcb');
			$('#s_page a').eq(1).css('color','#cbcbcb');
		}

	}

	$('#toComment').click(function(){
		$('#content').focus();
	});
	
});

function loader(f)
{
	if(f==1)
	{
		$('.load_mask').height($(document).height()+'px').show();
		$('.loader').show()
		
	}
	else if(f==2)
	{
		$('.load_mask').hide();
		$('.loader').hide()
	}
}
var gotop = document.getElementById('gotop');
var BodyScrollTop = 0;
var scrollTimer = null;
gotop.addEventListener('click',
function() {
	BodyScrollTop = document.body.scrollTop;
	if (BodyScrollTop > 0) {
		scrollTimer = setInterval(function() {
			BodyScrollTop -= 80;
			if (BodyScrollTop < 0) {
				BodyScrollTop = 0
			};
			document.body.scrollTop = BodyScrollTop;
			if (BodyScrollTop <= 0) {
				clearInterval(scrollTimer)
			};
			if (BodyScrollTop <= 250) {
				gotop.style.display = 'none';
			}
		},
		10)
	}
},
false);
document.addEventListener('touchmove',
function(e) {
	gotop.style.display = 'none';
},
false);
document.addEventListener('touchend',
function(e) {
	hideGoTop();
},
false);

function hideGoTop(){
	var top = document.body.scrollTop;
	if (top > 250) {
		gotop.style.display = 'block';
	}
}
window.onscroll=hideGoTop;